package tests;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import data.enums.Classification;
import data.enums.Index;
import data.input.Data;
import data.input.YahooCsv;
import entities.IndexTuple;

public class ArffCreator {

	public static void main(String[] args) throws Exception { 
		
		Data data = Data.createData(Index.FTSE_100);
		
		ArffCreator creator = new ArffCreator();
		Instances instances = creator.mapToInstances(data);
		
		System.out.println(instances);
		
	}
	
	public Instances mapToInstances (Data data) throws IOException {

		FastVector classifications = new FastVector();

		for (Classification classification : Classification.values()) {
			classifications.addElement(classification.toString());
		}
		
		//Attribute attDate = new Attribute("date", "dd/MM/yyyy");
		Attribute attrClose = new Attribute("close");
		Attribute attrClassification = new Attribute("classification", classifications);

		// Create a vector of attributes information.
		FastVector attributes = new FastVector();
		//attributes.addElement(attDate);
		attributes.addElement(attrClose);
		attributes.addElement(attrClassification);

		// Create an empty set of instances.
		Instances instances = new Instances(data.getIndexName().toString(), attributes, 0);

		for (IndexTuple tuple : data.getIndexTuples()) {
			
			Instance instance = new Instance(2);
			
//			instance.setValue(attDate, tuple.getDate().to);
			instance.setValue(attrClose, tuple.getClose().doubleValue());
			instance.setValue(attrClassification, tuple.getClassification().toString());
			instances.add(instance);
			
		}
		
		return instances;
		
	}

	private void save(YahooCsv csv, Instances instances) throws IOException {
		
		BufferedWriter writer = new BufferedWriter(new FileWriter(new File(csv.getIndexName().toString() + ".arff")));
		writer.write(instances.toString());
		writer.flush();
		writer.close();
	
	}
	
	
}
